package TUILayer; 
import ControlLayer.*;
import ModelLayer.*;
import java.util.Iterator;

public class LoanUI extends InputOutput
{
    private LoanCtr loanCtr;
    private AddressCtr addrCtr;
    private DVDCtr dvdCtr;

    public LoanUI() 
    {   
        loanCtr = LoanCtr.getInstance();   
        addrCtr = AddressCtr.getInstance();
        dvdCtr =  DVDCtr.getInstance();
    }

    public void start()  { loanMenu(); }

    public void loanMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            int choice =   writeLoanMenu();
            System.out.println("\f");
            switch(choice)
            {
                case 1  : addLoan();    break;
                case 2  : returnCopy(); break;
                case 3  : history(0);   break;
                case 4  : history(1);   break;
                default : exit=true;    break;
            }
        }
    }

    private int writeLoanMenu()
    {
        System.out.println("\n *** Loan Menu ***");
        System.out.println(" (1) Borrow copy");
        System.out.println(" (2) Return copy");
        System.out.println(" (3) Loan history");
        System.out.println(" (4) Copies lent out");
        System.out.println(" (5) Return");
        System.out.print("\n Make your choice: ");
        
        return readInt();
    }
    
    
    private void history(int ok)
    {
        Iterator it = loanCtr.getLoans();
        if(it.hasNext()==false) { System.out.println(" --> There is no history."); return; }
        
        int i = 0;
        while(it.hasNext())
        {
            Loan loan = (Loan) it.next();
            if(ok==1)
            {
                if(loan.getCopy().getReturned()==false) printLoan(loan,++i);
            }
            else printLoan(loan, ++i);
        }
    }
    
    private void printLoan(Loan loan, int i)
    {
        if(loan==null) return;
        System.out.println(" --> Loan (" + i + ")");
        System.out.println("     Borrow date: " + loan.getBorrowDate());
        System.out.println("     Remaining days: " );
        int days = loan.getRemainingDays();
        if(days<0) System.out.println((-days)+" days overdue");
        else System.out.println(days);
        
        Person person = addrCtr.getPerson(loan.getIDPerson());
        Dvd    dvd    = dvdCtr.getDVD(loan.getIDDvd());
        
        if(person==null) System.out.println("     The borrower was not found in the addressbook.");
        else System.out.println("     Borrower: " + person.getName() + " (id:" + person.getId() + ")");
        
        if(dvd==null) System.out.println("     The dvd was not found in the dvd collection.");
        else System.out.println("     DVD: " + dvd.getTitle() + " (id:" + dvd.getId() + ")");
        
    }
    
    
    private void returnCopy()
    {
        long idP = inputLong("Person ID");
        long idD = inputLong("Dvd ID");
        String serialNo = inputString("Serial No. of Copy");
        
        if(loanCtr.changeState(idP, idD, serialNo))
            System.out.println(" --> The copy was successfully marked as returned!");
        else
            System.out.println(" --> An error occurred while marking the copy as returned!");
            
        
    }

    private void addLoan()
    {     
        long idP = inputLong("Person ID");
        long idD = inputLong("DVD ID");
        int days = inputInt("Days to borrow");
        if(loanCtr.addLoan(idP,idD,days))
            System.out.println(" --> The loan was successfully created.");
        else
            System.out.println(" --> An error occured while creating the loan.");
            
    }
    
}